<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login_model extends CI_Model{
    function __construct(){
        parent::__construct();
    }
    
    public function validate(){
        // grab user input
        $username = $this->input->post('uname');
        $password = $this->input->post('upwd');
        
        // Prep the query
        $this->db->where('uname', $username);
        $this->db->where('upwd', do_hash($password, 'md5'));
        $this->db->where('deleted', '0');
        
        // Run the query
        $query = $this->db->get('users');
        // Let's check if there are any results
		//log_message('info', '$query->num_rows = ' . $query->num_rows);		
        if($query->num_rows == 1)
        {
            // If there is a user, then create session data
            $row = $query->row();
            $data = array(
				'userid' => $row->id,
				'username' => $row->uname,
				'email' => $row->email,
				'role' => $row->role,
				'validated' => true
            );
            $this->session->set_userdata($data);
            return true;
        }
        // If the previous process did not validate
        // then return false.
        return false;
    }
}
?>